package com.example.wm.controller;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wm.common.R;
import com.example.wm.common.Result;
import com.example.wm.common.ResultData;
import com.example.wm.entity.Complaints;
import com.example.wm.entity.Employee;
import com.example.wm.entity.Staffinandout;
import com.example.wm.mapper.StaffinandoutMapper;
import com.example.wm.service.CartinandoutService;
import com.example.wm.service.StaffinandoutService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@RestController
@RequestMapping("/Staffinandout")
//出入库
public class StaffinandoutController {
    @Resource
    private StaffinandoutService staffinandoutService;
        @Resource
    private StaffinandoutMapper staffinandoutMapper;
    @Transactional
    @PostMapping("/save")
    public Result saveStaff(@RequestBody Staffinandout staffinandout){
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
        Date date = new Date(System.currentTimeMillis());
        String time = formatter.format(date);
//       staffinandout.setTime(time);
       Boolean b= staffinandoutService.save(staffinandout);


        return ResultData.success(b);
    }
    @Transactional
    @PostMapping("/savemap")
    public Result saveStaffmap(@RequestBody List<Staffinandout> list ){
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
        Date date = new Date(System.currentTimeMillis());
        String time = formatter.format(date);
//       staffinandout.setTime(time);
//        Boolean b= staffinandoutService.save(staffinandout);
//        for(Map<Staffinandout, String> staff:list){
//            for(Map.Entry<Staffinandout, String> entry : staff.entrySet()) {
//                Staffinandout key = entry.getKey();
//                String value = entry.getValue();
//                // 在这里对每个键值对进行处理
//                // key 和 value 分别代表 Map 中的键和值
//                // 可以根据需要进行操作
//            }
//        }
      Boolean b= staffinandoutService.saveBatch(list);

        return ResultData.success(b);
    }
    @PostMapping("/transterurl")
    public Result<String> transterurl(@RequestBody MultipartFile multipartFile) throws IOException {

        byte[] ss=null;
        String base64EncodedString=null;
        try{
            ss = multipartFile.getBytes();
            base64EncodedString = Base64.getEncoder().encodeToString(ss);

        }catch(IOException e){
            e.printStackTrace();
        }
        String re=base64EncodedString;
        return  ResultData.success("操作成功",re);
    }
    @Transactional
    @PostMapping("/find")
    public Result findeStaff(@RequestBody Staffinandout staffinandout){

        LambdaQueryWrapper<Staffinandout> lq=new LambdaQueryWrapper<>();
        lq.groupBy(Staffinandout::getTime);
        lq.eq(Staffinandout::getName,staffinandout.getName());
        List<Staffinandout> list =staffinandoutService.list(lq);

//

        return ResultData.success(list);
    }


}

